package com.alexander.cloundexplorer.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.alexander.cloundexplorer.R;
import com.alexander.cloundexplorer.adapter.FilesAdapter;
import com.alexander.cloundexplorer.bean.params.LoginParams;
import com.alexander.cloundexplorer.bean.params.SearchFileWithNameAndSecondTypeIdParams;
import com.alexander.cloundexplorer.bean.params.SearchFileWithNameParams;
import com.alexander.cloundexplorer.bean.response.BaseResponse;
import com.alexander.cloundexplorer.bean.response.FileInfoResponse;
import com.alexander.cloundexplorer.util.Constants;
import com.alibaba.fastjson.JSON;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 * 按分类搜索文件(二级分类的ID)
 * */
public class SearchClassifyFileUnderOverallFragment extends BaseFragment implements FilesAdapter.OnItemClickLisnter
{
    private static SearchClassifyFileUnderOverallFragment searchClassifyFileFragment;
    private RelativeLayout inputKeywordLayout;
    private EditText keywordEditText;
    private RecyclerView recyclerView;
    private FilesAdapter filesAdapter;
    private List<FileInfoResponse> list = new ArrayList<>();
    private String secondTypeId = "";
    private LoginParams loginParams;

    public SearchClassifyFileUnderOverallFragment()
    {}

    public static SearchClassifyFileUnderOverallFragment getInstance(String secondTypeId, LoginParams loginParams)
    {
        searchClassifyFileFragment = new SearchClassifyFileUnderOverallFragment();
        searchClassifyFileFragment.secondTypeId = secondTypeId;
        searchClassifyFileFragment.loginParams = loginParams;
        return searchClassifyFileFragment;
    }

    @Override
    public void onAttach(Context context)
    {
        super.onAttach(context);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.fragment_searchclassifyfile_layout, container, false);
        initView(view);
        return view;
    }

    private void initView(View view)
    {
        inputKeywordLayout = view.findViewById(R.id.inputKeyword_Layout);
        keywordEditText = view.findViewById(R.id.keyword_EditText);
        recyclerView = view.findViewById(R.id.recyclerView);
        keywordEditText.setOnEditorActionListener(new TextView.OnEditorActionListener()
        {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event)
            {
                recyclerView.setVisibility(View.VISIBLE);
                inputKeywordLayout.setVisibility(View.GONE);
                String keyword = keywordEditText.getText().toString();
                Toast.makeText(getContext(),keyword , Toast.LENGTH_SHORT).show();
                String params = null;
                String url2 = null;
                /**某一类型下搜索*/
                SearchFileWithNameAndSecondTypeIdParams searchFileWithNameAndSecondTypeIdParams = new SearchFileWithNameAndSecondTypeIdParams(loginParams.getAccount(), loginParams.getPassword(), null, null);
                searchFileWithNameAndSecondTypeIdParams.setFileName(keyword);
                searchFileWithNameAndSecondTypeIdParams.setSecondTypeId(secondTypeId);
                params = JSON.toJSONString(searchFileWithNameAndSecondTypeIdParams);
                url2 = "fileController/searchFileUnderSecondTypeId";
                OkHttpUtils
                    .post()
                    .url(Constants.IP.concat(url2))
                    .addParams("params", params)
                    .build()
                    .execute(new StringCallback()
                    {
                        @Override
                        public void onError(Call call, Exception e, int id)
                        {
                            System.out.println(e.getMessage());
                        }
                        @Override
                        public void onResponse(String response, int id)
                        {
                            BaseResponse baseResponse = JSON.parseObject(response, BaseResponse.class);
                            list.removeAll(list);
                            list.addAll(JSON.parseArray(baseResponse.getData().toString(), FileInfoResponse.class));
                            filesAdapter.notifyDataSetChanged();
                        }
                    });
                return false;
            }
        });
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
        filesAdapter = new FilesAdapter(getContext(), list);
        filesAdapter.setOnItemClickLisnter(this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(filesAdapter);
    }

    @Override
    public void onResume()
    {
        super.onResume();
    }

    @Override
    public void onPause()
    {
        super.onPause();
    }

    @Override
    public void onDestroyView()
    {
        super.onDestroyView();
    }

    /**item点击回调和item的更多操作项被点击的回调*/
    @Override
    public void onItemClick(int position)
    {
    }
    @Override
    public void onMoreClick(int position)
    {
    }
}
